package com.lw.sbdemo2.common.web.formatter;

import com.lw.sbdemo2.common.utils.ValidUtil;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

/**
 * <p>标题: Long转Date格式化类</p>
 * <p>描述: 把long时间戳字符串转换为Date</p>
 * <p>版权: Copyright (c) 2016</p>
 * <p>公司: </p>
 *
 * @version: 1.0
 * @author: ffs
 */
public class LongToDateFormatter implements Formatter<Date> {
    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        if (ValidUtil.isEmptyOrNull(text)) {
            return null;
        }
        return new Date(Long.parseLong(text));
    }

    @Override
    public String print(Date object, Locale locale) {
        return String.valueOf(object.getTime());
    }
}
